import path from 'path';
import fs from 'fs';

import KoaRouter from '@koa/router';

const router: any[] = [];
const instance: Record<string, any> = {};
const routesDir = fs.readdirSync(path.resolve('src/router')).filter((item: string) => item.indexOf('v') >= 0);
routesDir.forEach((dirPath: string) => {
  instance[dirPath] = new KoaRouter({ prefix: `/${dirPath}` });
  const routesFiles = fs.readdirSync(path.resolve(path.resolve(`src/router/${dirPath}`))).filter((item: string) => item.indexOf('routes') >= 0);
  routesFiles.forEach((filePath: string) => {
    instance[dirPath].use(require(path.resolve(`src/router/${dirPath}/${filePath}`)).default.routes());
  });
  router.push(instance[dirPath].routes());
});

export default router;
